package bloggo

import (
	"context"
	"fmt"
	"log"
	"strings"
)

const (
	textBlack = iota + 30
	textRed
	textGreen
	textYellow
	textBlue
	textPurple
	textCyan
	textWhite
)

// Log 日志
func Log(str ...string) {
	log.Output(2, fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", textCyan, str))
}

// PrintSQL 打印sql
func PrintSQL(_ context.Context, sqlstr string, args []interface{}, execSQLMillis int64) {
	count := strings.Count(sqlstr, "?")
	for i := range args {
		sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("%v", args[i]), 1)
	}

	sqlstr = strings.Replace(strings.Replace(sqlstr, "\n", " ", -1), "\t\t\t", " ", -1)
	log.Output(5, fmt.Sprintf("sql:\x1b[0;%dm%s\x1b[0m", textPurple, sqlstr))
	if len(args) != count {
		log.Output(5, fmt.Sprintf("sql中的?与参数数量不匹配,输出的SQL应当仔细检查:\x1b[0;%dm%d\x1b[0m", textRed, execSQLMillis))
	}
	log.Output(5, fmt.Sprintf("execSQLMillis:\x1b[0;%dm%d\x1b[0m", textYellow, execSQLMillis))
}
